package cn.airfei.aircore.testcore.service.impl;

import cn.airfei.aircore.testcore.domain.convertor.UserConvertor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.airfei.aircore.testcore.services.service.impl.UserDomainServiceImpl;
import cn.airfei.aircore.testcore.services.entity.User;
import cn.airfei.aircore.testcore.storage.dto.UserDto;
import cn.airfei.aircore.testcore.storage.vo.UserVo;
import cn.airfei.aircore.testcore.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import cn.airfei.aircore.core.storage.PageParam;
import cn.airfei.aircore.core.enums.RespEnum;
import cn.airfei.aircore.core.exception.AppException;
import cn.airfei.aircore.core.utils.QueryParamUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author air
 * @since 2022-04-24
 */
@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserConvertor userConvertor;

    @Resource
    private UserDomainServiceImpl userDomainService;

    /**
     *
     * @param userDto
     * @param pageParam
     * @return
     */
    @Override
    public Page<UserVo> index(UserDto userDto,PageParam pageParam) {
        return userDomainService.index(userDto,pageParam);
    }

    /**
     * 分页查询单表数据
     *
     * @param map
     * @return
     */
    @Override
    public Page<UserVo> singleTablePage(Map<String, Object> map) {
        Page<User> userPage=userDomainService.singleTablePage(map);
        List<UserVo> userVoList = userConvertor.entityList2VoList(userPage.getRecords());
        Page userVoPage = userPage;
        userVoPage.setRecords(userVoList);
        return userVoPage;
    }

    /**
     * 显示单条数据
     *
     * @param id
     * @return
     */
    @Override
    public UserVo show(String id){
        User user = userDomainService.show(id);
        return userConvertor.entity2Vo(user);
    }

    /**
     * 删除（销毁表数据）
     *
     * @param ids
     * @return
     */
    @Override
    public Integer destroy(String ids){
        return userDomainService.destroy(ids);
    }

    /**
     * 更新表数据
     * @param userDto
     * @return
     */
    @Override
    public Integer update(UserDto userDto){
        Integer res = userDomainService.update(userConvertor.dto2Entity(userDto));
        if (res < 1) {
            throw new AppException(RespEnum.UPDATE_FAIL.getCode(), RespEnum.UPDATE_FAIL.getMsg());
        }
        return res;
    }

    /**
     * 保存表数据
     *
     * @param {entity?uncap_first}Dto
     * @return
     */
    @Override
    public Integer store(UserDto userDto){
        return userDomainService.store(userConvertor.dto2Entity(userDto));
    }

    /**
     * 设置状态，1:正常,2:禁用/待审核，-1:已删除
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean setStatus(Long id, Integer status) {
        if(userDomainService.setStatus(id,status)>0){
            return true;
        }
        throw new AppException(RespEnum.SET_STATUS_ERROR.getCode(),RespEnum.SET_STATUS_ERROR.getMsg());
    }

    /**
     * get all resource in storage
     *
     * @return
     */
    @Override
    public List<UserVo> getList(Map<String,Object> map) {
        QueryWrapper<User> queryWrapper = QueryParamUtil.getEntityWrapper(map, User.class);
        return userConvertor.entityList2VoList(userDomainService.list(queryWrapper));
    }

    /**
     * 更新多条数据（主键id必传）
     *
     * @return
     */
    @Override
    public List<UserVo> updateBatch(List<UserDto> userDtoList){
        List<User> userList = userConvertor.dtoList2EntityList(userDtoList);
        userDomainService.updateBatchById(userList);
        return userConvertor.entityList2VoList(userList);
    }

     /**
      * 添加多条数据
      *
      * @return
      */
    @Override
    public List<UserVo> saveBatch(List<UserDto> userDtoList){
        List<User> userList = userConvertor.dtoList2EntityList(userDtoList);
        userDomainService.saveBatch(userList);
        return userConvertor.entityList2VoList(userList);
    }


}
